package cn.gjxblog.demo2;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 作用: 自定义数据源，用来从数据库获取权限信息，用户信息
 * 作者:gjx
 * 版本:V1.0
 * 创建时间: 2018/7/26 8:54
 */
public class MyRealm extends AuthorizingRealm {
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        List<String> permission = new ArrayList<String>();
        permission.add("user:update");
        permission.add("user:delete");
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(permission);
        return  simpleAuthorizationInfo;

    }

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken upToken = (UsernamePasswordToken)token;

        String username = upToken.getUsername();
        if("xiaobai".equals(username)){
            return new SimpleAuthenticationInfo("xiaobai","123456",UUID.randomUUID().toString());
        }
        return null;
    }
}
